home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / Perl_WWW_Utilities / MHonArc / lib / mhexternal.pl < prev    next >
Encoding:
Perl Script  |  1996-04-16  |  9.7 KB  |  309 lines

  1. ##---------------------------------------------------------------------------##
  2. ##  File:
  3. ##      mhexternal.pl
  4. ##  Author:
  5. ##      Earl Hood       ehood@convex.com
  6. ##  Date:
  7. ##    Tue Apr 16 15:01:28 CDT 1996
  8. ##  Description:
  9. ##    Library defines a routine for MHonArc to filter content-types
  10. ##    that cannot be directly filtered into HTML, but a linked to an
  11. ##    external file.
  12. ##
  13. ##    Filter routine can be registered with the following:
  14. ##
  15. ##        <MIMEFILTERS>
  16. ##        */*:m2h_external'filter:mhexternal.pl
  17. ##        </MIMEFILTERS>
  18. ##
  19. ##    Where '*/*' represents various content-types.  See code below for
  20. ##    all subtypes.
  21. ##
  22. ##---------------------------------------------------------------------------##
  23. ##    MHonArc -- Internet mail-to-HTML converter
  24. ##    Copyright (C) 1995    Earl Hood, ehood@convex.com
  25. ##
  26. ##    This program is free software; you can redistribute it and/or modify
  27. ##    it under the terms of the GNU General Public License as published by
  28. ##    the Free Software Foundation; either version 2 of the License, or
  29. ##    (at your option) any later version.
  30. ##
  31. ##    This program is distributed in the hope that it will be useful,
  32. ##    but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34. ##    GNU General Public License for more details.
  35. ##
  36. ##    You should have received a copy of the GNU General Public License
  37. ##    along with this program; if not, write to the Free Software
  38. ##    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39. ##---------------------------------------------------------------------------##
  40.  
  41. package m2h_external;
  42.  
  43. ##---------------------------------------------------------------------------
  44. ##    Global variables
  45.  
  46. $pre = '';    # Prefix to generated files
  47. $ext = '';    # Extension for files
  48. $ctype = '';    # Content-type of data
  49. $TYPE = '';    # English name for data type
  50. %ExtCnt = ();    # Array of filename counters for generated files
  51. $inline = 0;    # Can be changed by filter argument
  52.  
  53. ##---------------------------------------------------------------------------
  54. ##    All that should be required is to add to, or edit, the %AppExt and
  55. ##    %AppType arrays for new, or changed, application suptypes.
  56.  
  57. $UnknownExt    = 'xxx';
  58. $UnknownType    = 'Unrecognized Data';
  59.  
  60. %CTExt = (
  61. ##  Content-Type            Filename extension
  62. ##---------------------------------------------------------
  63.     'application/mac-binhex40',     'hqx',
  64.     'application/octet-stream',     'bin',
  65.     'application/oda',             'oda',
  66.     'application/pdf',             'pdf',
  67.     'application/postscript',         'ps',
  68.     'application/rtf',             'rtf',
  69.     'application/x-bcpio',         'bcpio',
  70.     'application/x-cpio',         'cpio',
  71.     'application/x-csh',         'csh',
  72.     'application/x-dvi',         'dvi',
  73.     'application/x-gtar',         'gtar',
  74.     'application/x-hdf',         'hdf',
  75.     'application/x-latex',         'latex',
  76.     'application/x-mif',         'mif',
  77.     'application/x-netcdf',         'cdf',
  78.     'application/x-sh',         'sh',
  79.     'application/x-shar',         'shar',
  80.     'application/x-sv4cpio',         'sv4cpio',
  81.     'application/x-sv4crc',         'sv4crc',
  82.     'application/x-tar',         'tar',
  83.     'application/x-tcl',         'tcl',
  84.     'application/x-tex',         'tex',
  85.     'application/x-texinfo',         'texinfo',
  86.     'application/x-troff',         'roff',
  87.     'application/x-troff-man',         'man',
  88.     'application/x-troff-me',         'me',
  89.     'application/x-troff-ms',         'ms',
  90.     'application/x-ustar',         'ustar',
  91.     'application/x-wais-source',     'src',
  92.     'application/zip',             'zip',
  93.     'audio/basic',             'snd',
  94.     'audio/x-aiff',             'aif',
  95.     'audio/x-wav',             'wav',
  96.     'image/gif',            'gif',
  97.     'image/ief',            'ief',
  98.     'image/jpeg',            'jpg',
  99.     'image/tiff',            'tif',
  100.     'image/x-bmp',            'bmp',
  101.     'image/x-cmu-raster',        'ras',
  102.     'image/x-pict',            'pict',
  103.     'image/x-portable-anymap',        'pnm',
  104.     'image/x-pnm',            'pnm',
  105.     'image/x-portable-bitmap',        'pbm',
  106.     'image/x-pbm',            'pbm',
  107.     'image/x-portable-graymap',        'pgm',
  108.     'image/x-pcx',            'pcx',
  109.     'image/x-pgm',            'pgm',
  110.     'image/x-portable-pixmap',        'ppm',
  111.     'image/x-ppm',            'ppm',
  112.     'image/x-rgb',            'rgb',
  113.     'image/x-xbitmap',            'xbm',
  114.     'image/x-xbm',            'xbm',
  115.     'image/x-xpixmap',            'xpm',
  116.     'image/x-xpm',            'xpm',
  117.     'image/x-xwindowdump',        'xwd',
  118.     'image/x-xwd',            'xwd',
  119.     'video/mpeg',            'mpg',
  120.     'video/quicktime',            'mov',
  121.     'video/x-msvideo',            'avi',
  122.     'video/x-sgi-movie',        'movie',
  123. );
  124. %CTType = (
  125. ##  Content-Type            English name
  126. ##---------------------------------------------------------
  127.     'application/mac-binhex40',     'Mac BinHex file',
  128.     'application/octet-stream',     'Binary data',
  129.     'application/oda',             'ODA file',
  130.     'application/pdf',             'PDF file',
  131.     'application/postscript',         'PostScript file',
  132.     'application/rtf',             'RTF file',
  133.     'application/x-bcpio',         'BCPIO file',
  134.     'application/x-cpio',         'CPIO file',
  135.     'application/x-csh',         'Csh script',
  136.     'application/x-dvi',         'TeX dvi file',
  137.     'application/x-gtar',         'Gtar file',
  138.     'application/x-hdf',         'HDF file',
  139.     'application/x-latex',         'LaTex document',
  140.     'application/x-mif',         'Frame MIF',
  141.     'application/x-netcdf',         'Cdf file',
  142.     'application/x-sh',         'Sh script',
  143.     'application/x-shar',         'Shar file',
  144.     'application/x-sv4cpio',         'SV4Cpio file',
  145.     'application/x-sv4crc',         'SV4Crc file',
  146.     'application/x-tar',         'Tar file',
  147.     'application/x-tcl',         'Tcl script',
  148.     'application/x-tex',         'TeX document',
  149.     'application/x-texinfo',         'TeXInfo file',
  150.     'application/x-troff',         'Troff',
  151.     'application/x-troff-man',         'Troff Man',
  152.     'application/x-troff-me',         'Troff ME',
  153.     'application/x-troff-ms',         'Troff MS',
  154.     'application/x-ustar',         'UStar file',
  155.     'application/x-wais-source',     'WAIS Source',
  156.     'application/zip',             'Zip file',
  157.     'audio/basic',             'Basic audio',
  158.     'audio/x-aiff',             'AIF audio',
  159.     'audio/x-wav',             'WAV audio',
  160.     'image/gif',            'GIF image',
  161.     'image/ief',            'IEF image',
  162.     'image/jpeg',            'JPEG image',
  163.     'image/tiff',            'TIFF image',
  164.     'image/x-bmp',            'Windows bitmap',
  165.     'image/x-cmu-raster',        'CMU raster',
  166.     'image/x-pict',            'Mac PICT image',
  167.     'image/x-portable-anymap',        'Portable anymap',
  168.     'image/x-pnm',            'Portable anymap',
  169.     'image/x-portable-bitmap',        'Portable bitmap',
  170.     'image/x-pbm',            'Portable bitmap',
  171.     'image/x-portable-graymap',        'Portable graymap',
  172.     'image/x-pcx',            'PCX image',
  173.     'image/x-pgm',            'Portable graymap',
  174.     'image/x-portable-pixmap',        'Portable pixmap',
  175.     'image/x-ppm',            'Portable pixmap',
  176.     'image/x-rgb',            'RGB image',
  177.     'image/x-xbitmap',            'X bitmap',
  178.     'image/x-xbm',            'X bitmap',
  179.     'image/x-xpixmap',            'X pixmap',
  180.     'image/x-xpm',            'X pixmap',
  181.     'image/x-xwindowdump',        'X window dump',
  182.     'image/x-xwd',            'X window dump',
  183.     'video/mpeg',            'MPEG movie',
  184.     'video/quicktime',            'QuickTime movie',
  185.     'video/x-msvideo',            'MS video',
  186.     'video/x-sgi-movie',        'SGI Movie',
  187. );
  188.  
  189. ##---------------------------------------------------------------------------
  190. ##    Filter routine.
  191. ##
  192. ##    Argument string may contain the following values.  Each value
  193. ##    should be separated by a space:
  194. ##
  195. ##       inline    => Inline image data with IMG element.
  196. ##       usename    => Use name attribute for determining name
  197. ##               of derived file.  Use this option with caution
  198. ##               since it can lead to filename conflicts and
  199. ##               security problems.
  200. ##
  201. sub filter {
  202.     local($header, *fields, *data, $isdecode, $args) = @_;
  203.     local($ret, $filename, $nameparm);
  204.  
  205.     ## Init variables
  206.     $name = '';
  207.     $ctype = '';
  208.     $TYPE = '';
  209.     $ext = '';
  210.     $inline = 0;
  211.  
  212.     ## Get content-type
  213.     ($ctype) = $fields{'content-type'} =~ m%^\s*([\w-/]+)%;
  214.     $ctype =~ tr/A-Z/a-z/;
  215.  
  216.     ## See if name argument is to be used
  217.     ($nameparm) = $fields{'content-type'} =~ /name=(\S+)/i;
  218.     $nameparm =~ s/['"]//g;
  219.     $nameparm =~ s/.*[\/\\:]//;
  220.     if ($args =~ /usename/i) {
  221.     $name = $nameparm;
  222.     }
  223.  
  224.     ## Check if image inlining
  225.     $inline = ($args =~ /inline/i);
  226.  
  227.     ## Determine filename extension
  228.     $pre = $ext = $CTExt{$ctype};  $TYPE = $CTType{$ctype};
  229.     if (!$ext) {
  230.     $ext = $UnknownExt;
  231.     $TYPE = $UnknownType;
  232.     }
  233.  
  234.     ## Write file
  235.     $filename = &write_file(*data);
  236.  
  237.     ## Create HTML markup
  238.     if ($inline && ($ctype =~ /image/i)) {
  239.         $ret = join('', "<P>",
  240.                     &htmlize($fields{'content-description'}),
  241.                     "\n</P><P>\n",
  242.                     qq|<A HREF="$filename"><IMG SRC="$filename" |,
  243.                     qq|ALT="$TYPE"></a>\n|,
  244.             "</P>\n");
  245.     } else {
  246.         $ret = join('', "<P>\n",
  247.                     qq|<A HREF="$filename">|,
  248.                     &htmlize($fields{'content-description'}) || 
  249.                         $nameparm || $TYPE,
  250.                     "</A></P>\n");
  251.     }
  252.     ($ret, $filename);
  253. }
  254.  
  255. sub write_file {
  256.     local(*stuff) = shift;
  257.     local($fname, $tmp, $cnt) = ('', '');
  258.  
  259.     if ($name) {
  260.     $fname = $name;
  261.     } else {
  262.     if (!$ExtCnt{$ext}) { &set_cnt(); }
  263.     $cnt = $ExtCnt{$ext}++;
  264.     $fname = $pre . sprintf("%05d.",$cnt) . $ext;
  265.     }
  266.     $tmp = $'OUTDIR . $'DIRSEP . $fname;
  267.  
  268.     ## $'OUTDIR is set by MHonArc that specifies destination path
  269.     ## of filtered mail.
  270.     ##
  271.     if (open(OUTFILE, "> $tmp")) {
  272.     binmode(OUTFILE);        # For MS-DOS
  273.     print OUTFILE $stuff;
  274.     close(OUTFILE);
  275.     } else {
  276.     warn "Warning: Unable to create $tmp\n";
  277.     }
  278.     $fname;
  279. }
  280.  
  281. sub set_cnt {
  282.     local(@files) = ();
  283.     opendir(DIR, $'OUTDIR);
  284.     @files = sort numerically grep(/^$pre\d+\.$ext$/i, readdir(DIR));
  285.     close(DIR);
  286.     if (@files) {
  287.     ($ExtCnt{$ext}) = $files[$#files] =~ /(\d+)/;
  288.     $ExtCnt{$ext}++;
  289.     } else {
  290.     $ExtCnt{$ext} = 0;
  291.     }
  292. }
  293.  
  294. sub numerically {
  295.     ($A) = $a =~ /(\d+)/;
  296.     ($B) = $b =~ /(\d+)/;
  297.     $A <=> $B;
  298. }
  299.  
  300. sub htmlize {
  301.     local($txt) = shift;
  302.     $txt =~ s/&/\&/g;
  303.     $txt =~ s/>/>/g;
  304.     $txt =~ s/</</g;
  305.     $txt;
  306. }
  307.  
  308. 1;
  309.